package com.ycfmedical.bed.ui.fragment;

import com.ycfmedical.bed.R;
import com.ycfmedical.bed.utils.ResourceUtil;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.mvp.XLazyFragment;
import cn.droidlover.xstatecontroller.XStateController;

/**
 * Created by Egoist on 2017/2/8.
 */

public class WebFragment extends XLazyFragment
{
    String url;
    private static String baseUrls = ResourceUtil.getResources().getString(R.string.fragemnt_web_url);



    @BindView(R.id.fragment_web_view)
    WebView webView;

    @BindView(R.id.contentLayout)
    XStateController contentLayout;

    @Override
    public void initData(Bundle savedInstanceState)
    {
        initContentLayout();
        initWebView();
    }

    private void initContentLayout() {
        contentLayout.loadingView(View.inflate(context, R.layout.view_loading, null));
    }

    private void initWebView() {

        webView.setWebChromeClient(new WebChromeClient()
        {
            @Override
            public void onProgressChanged(WebView view, int newProgress)
            {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100)
                {
                    if (contentLayout != null)
                        contentLayout.showContent();
                    if (webView != null)
                        url = webView.getUrl();
                }
                else
                {
                    if (contentLayout != null)
                        contentLayout.showLoading();
                }
            }
        });
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setDatabaseEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.getSettings().setAppCacheEnabled(true);
        webView.loadUrl(WebFragment.baseUrls);
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_entertain_web;
    }
    
    @Override
    public Object newP()
    {
        return null;
    }
    
    public static WebFragment newInstance()
    {
        return new WebFragment();
    }

    @Override
    public void onPause()
    {
        super.onPause();
        if (webView != null)
            webView.onPause();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        if (webView != null)
            webView.onResume();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (webView != null)
        {
            webView.removeAllViews();
            webView.destroy();
        }
    }
}
